/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.swt.element.steuer.rollbar.gruppe.bild;

import java.io.IOException;
import java.io.RandomAccessFile;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;

import de.mnit.basis.fehler.Fehler;
import de.mnit.basis.swt.dnd.A_DND_ZielStd;
import de.mnit.basis.swt.dnd.DNDART;
import de.mnit.basis.swt.dnd.DNDTYP;
import de.mnit.basis.sys.event.S_Zuhoerer;

/**
 * @author Michael Nitsche
 *
 * TODO: http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/SWTClipboardExample.htm
 */
public class SWT_TextEditor extends A_SWT_Bild<SWT_TextEditor,StyledText> {

	public boolean zeilenmarkieren=true;
	public boolean zeilenumbruch=false;
	public boolean einzeilig_n_mehrzeilig=false;
	public boolean nurlesbar=false;


	public SWT_TextEditor() {
		s1_Rahmen(true);
	}


	// FUNKTION

	public SWT_TextEditor s1_ZeilenMarkieren() {
		PN_Erzeugt();
		this.zeilenmarkieren=true;
		return this;
	}

	public SWT_TextEditor s1_Zeilenumbruch() {
		PN_Erzeugt();
		this.zeilenumbruch=true;
		return this;
	}

	public SWT_TextEditor s1_Einzeilig() {
		PN_Erzeugt();
		this.einzeilig_n_mehrzeilig=true;
		return this;
	}

	public SWT_TextEditor s1_NurLesbar() {
		PN_Erzeugt();
		this.nurlesbar=true;
		return this;
	}

	public void sText(String text) {
		swt().setText(text);
	}

	public void sLeer() {
		swt().setText("");
	}

	public void anfuegen(String text) {
		Fehler.objekt.wenn_Null(text,1);
		swt().append(text);
	}

	public void auswahlAlles() {
		swt().selectAll();
	}

	public void auswahlNichts() {
		swt().setSelection(0,0);
	}

	public void systemAusschneiden() {
		swt().cut();
	}

	public void systemKopie() {
		swt().copy();
	}

	public void systemEinfuegen() {
		swt().paste();
	}

	public void sDropStandard() {
		this.sDND_Ziel(new A_DND_ZielStd(DNDTYP.TEXT, DNDTYP.DATEI, DNDART.KOPIE, DNDART.VERSCHIEBEN){
			public void aktion(DNDTYP typ, DNDART art, Object o, DropTargetEvent event) {
				if(typ==DNDTYP.TEXT) sText(""+o);
				else {
					String[] sa=(String[])o;
					if(sa==null || sa.length==0) return;
					try {
						StringBuilder sb=new StringBuilder();
						RandomAccessFile raf=new RandomAccessFile(sa[0], "r");
						while(raf.getFilePointer()<raf.length())
							sb.append((char)raf.read());
						raf.close();
						sText(sb.toString());
					}
					catch(IOException e)   { Fehler.direkt("Dateifehler", e, sa); } //Problem, da hier in Klassenimpl
				}
			}
		});
	}

	public void sFokus() {
		swt().setFocus();
	}

	/**
	 * null = Standard
	 * ""   = Nicht anzeigen
	 */
	public void sToolTip(String s) {
		swt().setToolTipText(s);
	}

	// LESEN

	public String gText() {
		return swt().getText();
	}

	public String gAuswahl() {
		return swt().getSelectionText();
	}

	public Integer gOffset(int x, int y) {
		final Point maus=new Point(x, y);
		try {
			return swt().getOffsetAtLocation(maus);
		}
		catch(IllegalArgumentException e) {
			return null;
		}
	}

	public StyleRange gOffsetStyle(int x, int y) {
		Integer offset=gOffset(x, y);
		if(offset==null)
			return null;
		StyleRange[] ranges=swt().getStyleRanges();
		for(StyleRange sr : ranges)
			if(offset>=sr.start && offset<sr.start+sr.length)
				return sr;
		return null;
	}

	// EVENT

	public void eGeaendert(S_Zuhoerer<String> z) {
		this.zuhoererPlus("AENDERUNG",z);
	}

	// ERZEUGEN

	protected int style6() {
		int style=0;
		if(this.zeilenmarkieren)	style=style | SWT.FULL_SELECTION;
		if(this.zeilenumbruch)      style=style | SWT.WRAP;
		if(this.nurlesbar)          style=style | SWT.READ_ONLY;
		return style | (this.einzeilig_n_mehrzeilig ? SWT.SINGLE : SWT.MULTI);
	}

	protected StyledText roh(Composite basis, int style) {
		return new StyledText(basis,style);
	}

	protected void init6() {
		this.eTaste(new KeyAdapter(){
			public void keyReleased(KeyEvent e) {
				if(e.keyCode=='a' && e.stateMask==SWT.CTRL) swt().selectAll();
			}
		});
		this.swt().addModifyListener(new ModifyListener(){
			public void modifyText(ModifyEvent e) {
				zuhoererStart("AENDERUNG",swt().getText());
				iScrollBars();
			}
		});
	}

	// INTERN

	private void iScrollBars() {
		if(this.swt().getVerticalBar()!=null && this.swt().getVerticalBar().isEnabled()) {
			int texthoehe=(this.swt().getLineCount()+1)*(this.swt().getLineHeight()+this.swt().getLineSpacing());
			this.swt().getVerticalBar().setVisible(texthoehe>=this.swt().getSize().y);
		}
	}

}
